<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	
	$m =  getParam('m','post');
	
	$result['sign'] = '0';
	
	$parm = requestArrayParm($_REQUEST);
	
	
	if($m == 'loginout')
	{
		setcookie('loginkey','',time()-86400,"/"); 
			
		$result['msg'] = '退出成功';
		$result['sign'] = '1';
		echoJSON($result);
		exit;
	}
	
	if($m == 'reg')
	{
		//注册流程
		$phone =  getParam('phone','post');
		$code =  getParam('code','post');
		$pwd =  getParam('pwd','post');
		
		if(empty($phone))
		{
			$result['msg'] = '请输入手机号';
			echoJSON($result);
			exit;
		}
		
		if(empty($code))
		{
			$result['msg'] = '请输入验证码';
			echoJSON($result);
			exit;
		}
		
		if(empty($pwd))
		{
			$result['msg'] = '请输入密码';
			echoJSON($result);
			exit;
		}
		
		unset($where);
		
		$where['phone'] = $phone;
		
		if($uservalid = _User::getUserValidCode($where))
		{
			
			if($uservalid['errornums'] >= 5)
			{
				$result['msg'] = '验证码错误次数过多';
				echoJSON($result);
				exit;
			}
			
			
			if($uservalid['code'] == $code)
			{
				unset($data);
				$data['loginname'] = $phone;
				$data['password'] = md5($pwd);
				$data['regtime'] = time();
				$data['source'] = '来源';
				$data['regip'] = getClientIP();
				
				if(time()<strtotime('2017-03-02'))
				{
					$data['account'] = 3;
				}
				
				
				
				$res = _User::addUserInfo($data);
				
				if(!empty($res))
				{
					//删除验证码
					unset($query);
					$query['phone'] = $phone;
					_User::delUserValidCode($query);
					
					$result['msg'] = '注册成功';
					$result['sign'] = '1';
					$result['url'] = SITE_URL.'index.php';
					
					unset($query,$userinfo);
					$query['id'] = $res;
					$userinfo = _User::getUserInfo($query);
					
					unset($query,$data);
					$query['id'] = $userinfo['id'];
					$data['lastnums'] = $userinfo['lastnums'] + 1;
					$data['lasttime'] = time();
					$data['lastip'] = getClientIP();
					
					$data['loginkey'] = md5($data['lasttime'].$userinfo['id'].$data['lastnums'].$data['lastip'].'!@#$%^&*()!@#$EW');
					
					_User::setUserInfo($query,$data);
					
					
					//写入session
					
					//$res
					unset($query,$userinfo);
					$query['id'] = $res;
					$userinfo = _User::getUserInfo($query);
					
					setUserInfo($userinfo);
					
					
				}
				else
				{
					$result['msg'] = '注册失败';
				}
			}
			else
			{
				unset($query,$data);
				$query['id'] = $uservalid['id'];
				$data['errornums'] = $uservalid['errornums']+1;
				
				_User::setUserValidCode($query, $data);
				$result['msg'] = '验证码输入错误';
				echoJSON($result);
				exit;
			}
		}
		else
		{
			$result['msg'] = '请先获取验证码';
			echoJSON($result);
			exit;
		}
		
		
		
	}
	
	
	if($m == 'login')
	{
		//登录流程
		
		$phone =  getParam('phone','post');
		$pwd =  getParam('pwd','post');
		
		if(empty($phone))
		{
			$result['msg'] = '请输入手机号';
			echoJSON($result);
			exit;
		}
		
		if(empty($pwd))
		{
			$result['msg'] = '请输入密码';
			echoJSON($result);
			exit;
		}
		
		unset($query);
		$query['loginname'] = $phone;
		
		if(!$userinfo = _User::getUserInfo($query))
		{
			$result['msg'] = '手机号未注册';
			echoJSON($result);
			exit;
		}
		else
		{
			if(md5($pwd) == $userinfo['password'])
			{
				//更新登录次数及ip
				if($userinfo['status'] == '1')
				{
					unset($query,$data);
					$query['id'] = $userinfo['id'];
					$data['lastnums'] = $userinfo['lastnums'] + 1;
					$data['lasttime'] = time();
					$data['lastip'] = getClientIP();
					
					$data['loginkey'] = md5($data['lasttime'].$userinfo['id'].$data['lastnums'].$data['lastip'].'!@#$%^&*()!@#$EW');
					
					_User::setUserInfo($query,$data);
					
					unset($query);
					$query['id'] = $userinfo['id'];
					$userinfo = _User::getUserInfo($query);
					
					setUserInfo($userinfo);
					
					$result['msg'] = '登录成功';
					$result['sign'] = '1';
					$result['url'] = SITE_URL.'index.php';
					echoJSON($result);
					exit;
				}
				else
				{
					$result['msg'] = '帐号被禁用';
					echoJSON($result);
					exit;
				}
			}
			else
			{
				$result['msg'] = '密码错误';
				echoJSON($result);
				exit;
			}
		}
		
		
		
		
	}
	
	
	if($m == 'forget')
	{
		//忘记密码流程
		$phone =  getParam('phone','post');
		$code =  getParam('code','post');
		$pwd =  getParam('pwd','post');
		$pwdtoo =  getParam('pwdtoo','post');
		if(empty($phone))
		{
			$result['msg'] = '请输入手机号';
			echoJSON($result);
			exit;
		}
		
		if(empty($code))
		{
			$result['msg'] = '请输入验证码';
			echoJSON($result);
			exit;
		}
		
		if(empty($pwd))
		{
			$result['msg'] = '请输入密码';
			echoJSON($result);
			exit;
		}
		
		
		unset($where);
		
		$where['phone'] = $phone;
		
		if($uservalid = _User::getUserValidCode($where))
		{
			
			if($uservalid['errornums'] >= 5)
			{
				$result['msg'] = '验证码错误次数过多';
				echoJSON($result);
				exit;
			}
			
			
			if($uservalid['code'] == $code)
			{
				unset($query);
		
				$query['loginname'] = $phone;
				
				if($userinfo = _User::getUserInfo($query))
				{
					unset($query,$data);
					
					$query['id'] = $userinfo['id'];
					
					$data['password'] = md5($pwd); 
					
					$res = _user::setUserInfo($query, $data);
					if(!empty($res))
					{
						//删除验证码
						unset($query);
						$query['phone'] = $phone;
						_User::delUserValidCode($query);
						
						$result['msg'] = '重置密码成功';
						$result['sign'] = '1';
						$result['url'] = '?user.php?a=login';//跳转到登陆页面
					}
					else
					{
						$result['msg'] = '重置密码失败';
					}
				}
				else
				{
					$result['msg'] = '系统异常';
					echoJSON($result);
					exit;
				}
			}
			else
			{
				unset($query,$data);
				$query['id'] = $uservalid['id'];
				$data['errornums'] = $uservalid['errornums']+1;
				
				_User::setUserValidCode($query, $data);
				$result['msg'] = '验证码输入错误';
				echoJSON($result);
				exit;
			}
		}
		else
		{
			$result['msg'] = '请先获取验证码';
			echoJSON($result);
			exit;
		}
	}
	
	if($m == 'editpwd')
	{
		//修改密码流程
		$code =  getParam('code','post');
		$pwd =  getParam('pwd','post');
		$pwdtoo =  getParam('pwdtoo','post');
		
		
		if(empty($code))
		{
			$result['msg'] = '请输入验证码';
			echoJSON($result);
			exit;
		}
		
		if(empty($pwd))
		{
			$result['msg'] = '请输入密码';
			echoJSON($result);
			exit;
		}
		
		$phone = getUserInfo('loginname');
		
		unset($where);
		
		$where['phone'] = $phone;
		
		if($uservalid = _User::getUserValidCode($where))
		{
			
			if($uservalid['errornums'] >= 5)
			{
				$result['msg'] = '验证码错误次数过多';
				echoJSON($result);
				exit;
			}
			
			
			if($uservalid['code'] == $code)
			{
				unset($query);
		
				$userinfo = getUserInfo();
				if(!empty($userinfo))
				{
					unset($query,$data);
					
					$query['id'] = $userinfo['id'];
					
					$data['password'] = md5($pwd); 
					
					$res = _user::setUserInfo($query, $data);
					if(!empty($res))
					{
						//删除验证码
						unset($query);
						$query['phone'] = $phone;
						_User::delUserValidCode($query);
						
						$result['msg'] = '重置密码成功';
						$result['sign'] = '1';
						$result['url'] = '?user.php?a=login';//跳转到登陆页面
					}
					else
					{
						$result['msg'] = '重置密码失败';
					}
				}
				else
				{
					$result['msg'] = '系统异常';
					echoJSON($result);
					exit;
				}
			}
			else
			{
				unset($query,$data);
				$query['id'] = $uservalid['id'];
				$data['errornums'] = $uservalid['errornums']+1;
				
				_User::setUserValidCode($query, $data);
				$result['msg'] = '验证码输入错误';
				echoJSON($result);
				exit;
			}
		}
		else
		{
			$result['msg'] = '请先获取验证码';
			echoJSON($result);
			exit;
		}
	}
	
	
	if($m == 'send')
	{
		//获取验证码流程
		$phone =  getParam('phone','post');
		
		$type =  getParam('type','post');
		
		if(!in_array($type, array('reg','forget','editpwd')))
		{
			$result['msg'] = '非法获取验证码';
			echoJSON($result);
			exit;
		}
		
		
		if($type == 'editpwd')
		{
			$phone = getUserInfo('loginname');//session获取
		}
		
		if(empty($phone))
		{
			$result['msg'] = '请输入手机号';
			echoJSON($result);
			exit;
		}
		
		//根据类型判断手机号是否已存在
		
		if($type == 'reg')
		{
			unset($query);
			$query['loginname'] = $phone;
			
			if($userinfo = _User::getUserInfo($query))
			{
				$result['msg'] = '手机号已注册';
				echoJSON($result);
				exit;
			}
		}
		
		if($type == 'forget')
		{
			unset($query);
			$query['loginname'] = $phone;
			
			if(!$userinfo = _User::getUserInfo($query))
			{
				$result['msg'] = '手机号没有注册';
				echoJSON($result);
				exit;
			}
			else
			{
				if($userinfo['status'] != '1')
				{
					$result['msg'] = '帐号已被禁用';
					echoJSON($result);
					exit;
				}
			}
		}
		
		unset($where);
		
		$where['phone'] = $phone;
		
		if($uservalid = _User::getUserValidCode($where))
		{
			//验证码发送规则
			
			if($uservalid['sendnums'] >= 50)
			{
				$result['msg'] = '超出获取最大次数';
				echoJSON($result);
				exit;
			}
			else
			{
				//调用发送短信接口,发送验证码
			
				$msg['phone'] = $phone;
				$msg['msg'] =  '【伏蒙理财】验证码为：'.$uservalid['code'].'；如非本人操作，请忽略此短信。';
				
				sendMsg($msg);
				
				unset($query,$data);
				$query['id'] = $uservalid['id'];
				$data['sendnums'] = $uservalid['sendnums']+1;
				
				_User::setUserValidCode($query, $data);
				
				$result['sign'] = '1';
				$result['msg'] = '获取验证码成功';
			}
			
			
			
		}
		else
		{
			$code  = getcode(4);
			
			unset($data);
			
			$data['phone'] = $phone;
			
			$data['code'] = $code;
			
			$data['optime'] = time();
			
			$res = _User::addUserValidCode($data);
			
			if(!empty($res))
			{
				//调用发送短信接口,发送验证码//// 修改密码的正在想内容
				
				$msg['phone'] = $phone;
				$msg['msg'] =  '【伏蒙理财】验证码为：'.$code.'；如非本人操作，请忽略此短信。';
				
				sendMsg($msg);
				
				$result['sign'] = '1';
				$result['msg'] = '获取验证码成功';
			}
			else
			{
				$result['msg'] = '获取验证码失败';
			}
		}
	}

	echoJSON($result);
